package util;

import org.junit.Test;

import java.util.LinkedHashMap;

/**
 * LinkedHashMap继承HashMap
 * 遍历时默认可以保持插入顺序访问，通过构造函数accessOrder=true设置成访问顺序
 * @Author tuyou_cd
 * @Date 2020/8/12 14:58
 */
public class LinkedHashMapTest {

    @Test
    public void testAccessOrderFalse() {
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put(2, 2);
        linkedHashMap.put(1, 1);
        linkedHashMap.put(4, 4);
        linkedHashMap.put(3, 3);

        for (Object o : linkedHashMap.entrySet()) {
            System.out.println(o);
        }
    }

    @Test
    public void testAccessOrderTrue() {
        LinkedHashMap linkedHashMap = new LinkedHashMap(10, .75f, true);
        linkedHashMap.put(2, 2);
        linkedHashMap.put(1, 1);
        linkedHashMap.put(4, 4);
        linkedHashMap.put(3, 3);

        System.out.println("------------");
        for (Object o : linkedHashMap.entrySet()) {
            System.out.println(o);
        }

        linkedHashMap.get(1);
        linkedHashMap.get(2);

        System.out.println("------------");
        for (Object o : linkedHashMap.entrySet()) {
            System.out.println(o);
        }
    }
}
